import axios from 'axios';
import { Message, Loading } from 'element-ui';

console.log(process.env.NODE_ENV,process.env.BASE_URL)
const service = axios.create({
  //baseURL: process.env.BASE_URL,
  //baseURL:'http://192.168.1.16:8080',
  baseURL: process.env.NODE_ENV == "development" ? "": process.env.BASE_URL,
   //baseURL: BASE_URL,
  // baseURL: 'http://127.0.0.1',
  //baseURL: 'http://dev.oa.com',
  //baseURL: process.env.VUE_APP_API_URL,
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 12000000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    let access_token = sessionStorage.getItem('access_token')
    if(access_token != null){
      config.headers['Authorization'] = access_token
    }
    return config
  },
  error => {
    // do something with request error
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    const res = response.data;
    if (res.state === 'err') {
      // 将错误信息通过 Promise.reject 传递到 error 处理中
      const customError = new Error(res.msg);
      customError.type = 'API_ERROR'; // 添加自定义错误类型标识
      return Promise.reject(customError);    }
    return res;
  },
  error => {
    // 所有错误（包括响应错误和主动抛出的错误）都会到这里
    return Promise.reject(error);
  }
)
export default service

